package Or

import (
	"database/sql"
	"fmt"
	"db2s/ETL"
	"db2s/global"
	"db2s/ref"
	"strings"
)

func (fm ForeignMe) Set(parameter string, value any) (err error) {
	return ref.UpdateStructJsonValue(fm, parameter, value)
}

func (fm ForeignMe) Result(object string) global.Return {
	switch object {
	default:
		return global.Return{}
	}
}
func (fm ForeignMe) Name(Con *sql.DB) (result global.Return, err error) {
	var event = "[Name]"
	var execSql []string
	fm.Parameter.Con = Con
	for _, v := range []string{"all_constraints", "dba_constraints"} {
		execSql = append(execSql, fmt.Sprintf("select CONSTRAINT_NAME from %v where CONSTRAINT_TYPE='R' AND  OWNER = '%v' %v",
			v, fm.Parameter.Object.Schema, func() string {
				switch {
				case fm.Parameter.Object.Table == "*":
					return ""
				case strings.Contains(fm.Parameter.Object.Table, "%"):
					return fmt.Sprintf(" and TRIGGER_NAME like '%%%v%%'", fm.Parameter.Object.Table)
				default:
					return fmt.Sprintf(" and TRIGGER_NAME = '%v'", fm.Parameter.Object.Table)
				}
			}()))
	}
	if result, err = forExecQuerySql(event, fm.Parameter, ETL.StringGarth, execSql); err != nil || result.Result == nil {
		return
	}
	return
}
func (fm ForeignMe) Existence(Con *sql.DB) (result global.Return, err error) {
	var event = "[Existence]"
	var execSql []string
	fm.Parameter.Con = Con
	for _, v := range []string{"all_constraints", "dba_constraints"} {
		execSql = append(execSql, fmt.Sprintf("select CONSTRAINT_NAME from %v where CONSTRAINT_TYPE='R' AND  OWNER = '%v' AND CONSTRAINT_NAME='%v'", v, fm.Parameter.Object.Schema, fm.Parameter.Object.Table))
	}
	if result, err = forExecQuerySql(event, fm.Parameter, ETL.String, execSql); err != nil || result.Result == nil {
		return
	}
	switch {
	case strings.EqualFold(fmt.Sprintf("%v", result.Result), fm.Parameter.Object.Table):
		result.Result = true
	default:
		result.Result = false
	}
	return
}
func (fm ForeignMe) ForeignDefinition(Con *sql.DB) (result global.Return, err error) {
	return
}

